package com.danger.Presenter;

import android.content.Context;

import com.danger.Model.action.BindMobileModel;
import com.danger.Model.action.BindMobileModelImpl;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Model.listener.OnRegisterListener;
import com.danger.Model.listener.OnSendCheckNumListener;
import com.danger.Model.listener.OnVerifyCheckNumListener;
import com.danger.Util.CheckCode;
import com.danger.View.BindMobileView;

import cn.bmob.v3.exception.BmobException;

/**
 * Created by danger on 2016/3/18.
 */
public class BindMobilePresenterImpl implements BindMobilePresenter {

    private BindMobileView bindMobileView;
    private BindMobileModel bindMobileModel;
    private Context context;
    private boolean send;
    private boolean flag;

    public BindMobilePresenterImpl(Context context, BindMobileView bindMobileView) {
        this.context = context;
        this.bindMobileView = bindMobileView;
        bindMobileModel = new BindMobileModelImpl(context);
    }

    @Override
    public void sendCode(final String phone) {
        String msg = bindMobileModel.checkPhone(phone);
        if (msg.equals("正确")) {
            bindMobileModel.sendChecknum(phone, new OnSendCheckNumListener() {
                @Override
                public void onSuccess() {
                    send = true;
                    bindMobileView.showMessage("验证码发送成功");
                    bindMobileView.getSendButton().setClickable(false);
                }

                @Override
                public void onError(BmobException e) {
                    send = false;
                    bindMobileView.showError(CheckCode.checkByCode(e.getErrorCode()));
                }
            });
        } else {
            send = false;
            bindMobileView.showMessage(msg);
        }
    }

    @Override
    public void register(final String phone, String code) {
        String msg = bindMobileModel.check(phone, code);
        if (msg.equals("正确")) {
            final UserInfoBean userInfo = bindMobileView.getUserInfo();
            bindMobileView.showDialog();
            if (send) {
                bindMobileModel.verifyChecknum(phone, code, new OnVerifyCheckNumListener() {
                    @Override
                    public void onSuccess() {
                        bindMobileModel.register(userInfo, phone, new OnRegisterListener() {
                            @Override
                            public void onSuccess() {
                                bindMobileView.hideDialog();
                                bindMobileView.showMessage("注册成功");
                                bindMobileView.toLogin();
                            }

                            @Override
                            public void onError(String msg) {
                                bindMobileView.hideDialog();
                                bindMobileView.showMessage("注册失败，请检查验证码是否正确");
                            }
                        });
                    }

                    @Override
                    public void onError(BmobException e) {
                        bindMobileView.hideDialog();
                        bindMobileView.showError(CheckCode.checkByCode(e.getErrorCode()));
                    }
                });
            } else {
                bindMobileView.showMessage("注册失败");
            }
        } else {
            bindMobileView.showMessage(msg);
        }
    }
}
